<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>销售退货订单记录（仓库）</title>
<script type="text/javascript">
    var editSaleOrderWin;
    var saleReturnedAssociateAuditWin;
    var returnedEntryWin;

    var saleReturnedId;
    var currentOrderNumber;//当前正操作的销售单单号
    Ext.onReady(function () {

        var customer = Ext.create('Ext.form.ComboBox', {
            fieldLabel: '客户',
            labelWidth: 35,
            store: customerStore,
            valueField: 'id',
            displayField: 'name',
            name: 'customer_id',
            loadingText: '搜索中...',
            pageSize: 10,
            minChars: 0,
            anchor: '100%',
            listConfig: {
                getInnerTpl: function () {
                    return '<div data-qtip="{number}">{name}</div>';
                }
            }
        });

        var orderNumber = Ext.create('Ext.form.field.Text', {
            fieldLabel: '单号',
            name: 'order_number',
            anchor: '95%'
        });

        var auditingStatus = Ext.create('Ext.form.ComboBox', {
            fieldLabel: '审批状态',
            store: auditStatusStore,
            displayField: 'text',
            typeAhead: true,
            mode: 'local',
            name: 'auditing_status',
            forceSelection: true,
            editable: false,
            valueField: 'value',
            anchor: '95%',
            emptyText: ''
        })

        var beginDate = Ext.create('Ext.form.field.Date', {
            fieldLabel: '退货日期',
            name: 'begin_date',
            id: 'begin_date',
            format: 'Y-m-d', //格式化日期
            //disabledDays: [0,6],	//不让选择周六，周日
            //disabledDaysText: "周末要休息",
            invalidText: "您输入的日期格式不对，正确格式为2010-01-01",
            anchor: '95%',
            vtype: 'daterange',
            endDateField: 'end_date'
        });

        var endDate = Ext.create('Ext.form.field.Date', {
            fieldLabel: '至',
            name: 'end_date',
            id: 'end_date',
            format: 'Y-m-d', //格式化日期
            width: '100',
            //disabledDays: [0,6],	//不让选择周六，周日
            //disabledDaysText: "周末要休息",
            invalidText: "您输入的日期格式不对，正确格式为2010-01-01",
            anchor: '95%',
            vtype: 'daterange',
            startDateField: 'begin_date'
        });

        //产品
        var productCBB = Ext.create('Ext.form.ComboBox', {
            store: productStore,
            labelWidth: 35,
            fieldLabel: '产品',
            emptyText: '请选择...',
            valueField: 'id',
            displayField: 'name',
            name: 'product_id',
            loadingText: '搜索中...',
            anchor: '100%',
            pageSize: 10,
            minChars: 0,
            listConfig: {
                loadingText: 'Searching...',
                getInnerTpl: function () {
                    return '<div style="float:left; width:200px" data-qtip="{name}">{name:ellipsis(18)}</div><div style="float:left; width:110px">{number}</div>{is_suit}';
                }
            }
        });

        var submitButton = Ext.create('Ext.Button', {
            text: '搜索',
            iconCls: 'finding',
            handler: function () {
                saleReturnedDS.removeAll();

                saleReturnedDS.getProxy().extraParams = queryFormPanel.getForm().getFieldValues();
                toolBar.moveFirst();
            }
        });

        var clearButton = Ext.create('Ext.Button', {
            text: '清空',
            width: 50,
            handler: function () {
                queryFormPanel.form.reset();
            }
        });

        var queryFormPanel = Ext.create('Ext.form.Panel', {
            fieldDefaults: {
                labelWidth: 65,
                labelAlign: 'right'
            },
            width: screenWidth - 165,
            frame: true,
            border: false,
            items: [
                {
                    xtype: 'fieldset',
                    defaults: {width: 1000},
                    title: '查询条件',
                    border: true,
                    items: [
                        {
                            layout: 'column',
                            xtype: 'container',
                            items: [
                                {
                                    columnWidth: .2,
                                    layout: 'anchor',
                                    xtype: 'container',
                                    items: [orderNumber]
                                },
                                {
                                    columnWidth: .2,
                                    layout: 'anchor',
                                    xtype: 'container',
                                    items: [auditingStatus]
                                },
                                {
                                    columnWidth: .4,
                                    layout: 'anchor',
                                    xtype: 'container',
                                    items: [customer]
                                }
                            ]
                        },
                        {
                            layout: 'column',
                            xtype: 'container',
                            items: [
                                {
                                    columnWidth: .2,
                                    layout: 'anchor',
                                    xtype: 'container',
                                    items: [beginDate]
                                },
                                {
                                    columnWidth: .2,
                                    layout: 'anchor',
                                    xtype: 'container',
                                    items: [endDate]
                                },
                                {
                                    columnWidth: .4,
                                    layout: 'anchor',
                                    xtype: 'container',
                                    items: [productCBB]
                                },
                                {
                                    columnWidth: .1,
                                    layout: 'anchor',
                                    xtype: 'container',
                                    items: [submitButton]
                                },
                                {
                                    columnWidth: .1,
                                    layout: 'anchor',
                                    xtype: 'container',
                                    items: [clearButton]
                                }
                            ]
                        }
                    ]
                }
            ]
        });
        //显示销售退货订单操作时的窗口介面。
        //----------------------------------销售退货订单Grid-------------------------
        //销售退货订单Grid Store
        var saleReturnedDS = Ext.create('Ext.data.Store', {
            //autoDestroy : true,
            model: 'SaleReturnedModel',
            proxy: {
                type: 'ajax',
                url: '/sale_returned/getAllSaleReturneds',
                reader: {
                    type: 'json',
                    root: 'root',
                    totalProperty: 'totalProperty'// 数据集记录总数
                }
            },
            autoLoad: true,
            pageSize: pageSize
        });

        var toolBar = Ext.create('Ext.toolbar.Paging', {
            pageSize: pageSize,
            store: saleReturnedDS,
            displayInfo: true,
            displayMsg: "显示第 {0} 条到 {1} 条记录，一共 {2} 条",
            emptyMsg: "没有记录",
            items: ["-", {
                text: '撤消',
                iconCls: 'cancel',
                handler: function () {
                    var records = saleReturnedGrid.getSelectionModel().getSelection();
                    if (records.length == 0) {
                        return false;
                    } else {
                        if (records[0].get('deal_with_status') != '已撤消') {
                            var orderNumber = records[0].get('order_number')
                            saleReturnedId = records[0].get('id');
                            Ext.MessageBox.confirm("提示", "你确认要申请撤消订单号为： \"" + orderNumber + "\" 的销售返库单吗？", function (btn) {
                                if (btn == "yes") {
                                    Ext.Ajax.request({
                                        url: '/sale_returned/cancelEntry?id=' + saleReturnedId,
                                        success: function (result, request) {
                                            saleReturnedDS.load();
                                        },
                                        failure: function (result, request) {
                                            Ext.MessageBox.show({
                                                title: '消息',
                                                msg: "通讯失败，请从新操作",
                                                buttons: Ext.MessageBox.OK,
                                                icon: Ext.MessageBox.WARNING
                                            });
                                        },
                                        method: 'GET'
                                    });
                                } else {
                                    //alert("no");
                                }
                            });
                        }
                    }
                }
            }, '-',
                {
                    text: '打印',
                    iconCls: 'print',
                    handler: function () {
                        var records = saleReturnedGrid.getSelectionModel().getSelection();
                        if (records.length == 0) {
                            return false;
                        } else {
                            if (records[0].get('store_status').indexOf('完毕') > 0 && records[0].get('auditing_status').indexOf('申请') == -1 && records[0].get('auditing_status').indexOf('撤消') == -1 && records[0].get('auditing_status').indexOf('驳回') == -1) {
                                this.disable();
                                saleReturnedId = records[0].get('id');
                                //LODOP.SET_PRINT_PAGESIZE(1,0,0,'A5')
                                LODOP.PRINT_INIT("");
                                LODOP.SET_PRINT_PAGESIZE(1, "210mm", "148mm", "")
                                //LODOP.SET_PRINT_PAGESIZE(1,"148mm","210mm","")
                                LODOP.ADD_PRINT_URL(20, '5%', "90%", "100%", 'http://' + window.location.host + '/sale_returned/sale_returned_print_header?sub_type=1&id=' + saleReturnedId);
                                LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);
                                LODOP.ADD_PRINT_TBURL(145, '5%', "90%", "43%", 'http://' + window.location.host + '/sale_returned/sale_returned_print_content?sub_type=1&id=' + saleReturnedId);
                                LODOP.SET_PRINT_STYLEA(0, "ItemType", 4);
                                LODOP.ADD_PRINT_URL(395, '5%', "90%", "100%", 'http://' + window.location.host + '/sale_returned/sale_returned_print_footer?sub_type=1&id=' + saleReturnedId);
                                LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);
                                LODOP.ADD_PRINT_TEXT(480, '8%', "80%", "8%", "第一联：成本（白） 第二联：财务（绿） 第三联：提货（粉） 第四联：客户（黄） 第五联：销售（兰）");
                                LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);
                                LODOP.ADD_PRINT_TEXT(500, '35%', "90%", "8%", "页号：第#页/共&页 打印日期：" + getDate());
                                LODOP.SET_PRINT_STYLEA(0, "ItemType", 2);
                                LODOP.PREVIEW();
                                this.enable();
                                //window.open('/test/sale_order_print?id='+saleOrderId,'_blank','width=0,height=0,toolbar=no,menubar=no,location=no');
                            }
                        }
                    }
                }]
        })

        //生成要订单的产品的Grid
        var saleReturnedGrid = Ext.create('Ext.grid.Panel', {
            title: '销售退货订单列表',
            store: saleReturnedDS,
            columns: [Ext.create('Ext.grid.RowNumberer'), {
                header: 'DB',
                dataIndex: 'id',
                width: 50
            }, {
                header: '公司',
                dataIndex: 'company_name',
                width: 50
            }, {
                header: '销售退货订单单号',
                dataIndex: 'order_number',
                width: 120
            }, {
                header: '销售订单单号', dataIndex: 'sale_order_number', width: 210
            }, {
                header: '供应商',
                dataIndex: 'customer_name',
                width: 150
            }, {
                header: '总金额',
                dataIndex: 'amount',
                align: 'right',
                renderer: moneyPrivilegeRender,
                width: 75
            }, {
                header: '已付金额',
                dataIndex: 'paid_amount',
                align: 'right',
                summaryType: 'sum',
                width: 75
            }, {
                header: '退货日期',
                dataIndex: 'return_date',
                renderer: Ext.util.Format.dateRenderer('Y-m-d'),
                width: 80
            }, {
                header: '退货入库日期',
                dataIndex: 'action_date',
                renderer: Ext.util.Format.dateRenderer('Y-m-d'),
                width: 90
            }, {
                header: '入库仓库',
                dataIndex: 'warehouse_name',
                width: 90
            }, {
                header: '操作员',
                dataIndex: 'employee_name',
                width: 60
            }, {
                header: '部门',
                dataIndex: 'department_name',
                width: 70
            }, {
                header: '销售退货订单审批状态',
                dataIndex: 'auditing_status',
                width: 130,
                renderer: auditingStatusRender
            }, {
                header: '内勤审批者',
                dataIndex: 'associate_auditor_name',
                width: 75
            }, {
                header: '内勤审批意见',
                dataIndex: 'associate_audit_opinion',
                width: 150
            }, {
                header: '处理状态',
                dataIndex: 'deal_with_status',
                renderer: dealWithStatusRender,
                width: 70
            }, {
                header: '入库状态',
                dataIndex: 'store_status',
                renderer: dealWithStatusRender,
                width: 70
            }, {
                header: '仓库操作员',
                dataIndex: 'warehouse_employee_name',
                width: 70
            }, {
                header: '生成日期',
                dataIndex: 'created_at',
                renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),
                width: 115
            }, {
                header: '修改日期',
                dataIndex: 'updated_at',
                renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),
                width: 115
            }],
            selModel: Ext.create('Ext.selection.CheckboxModel'),
            loadMask: true,
            width: screenWidth - 165,
            height: 315,
            frame: true,
            //title: '进货品种列表',
            //plain:true,
            //plugins: checkColumn,
            tbar: toolBar
        });

        saleReturnedGrid.on('itemdblclick', function (gridPanel, record, item, index, e, options) {
            saleReturnedId = saleReturnedDS.getAt(index).get('id');
            var saleReturnedNumber = saleReturnedDS.getAt(index).get('order_number');
            //saleReturnedDS.load();
            saleReturnedDetailDS.removeAll();
            saleReturnedDetailDS.load({params: {'sale_returned_id': saleReturnedId}});
            saleReturnedDetailGrid.setTitle("销售退货订单：" + saleReturnedNumber + "，的详细信息");
        });

        //右键菜单，以弹出此订单相关的入库单
        //右键
        saleReturnedGrid.on('itemcontextmenu', function (grid, record, item, index, e, options) {
            saleReturnedId = saleReturnedDS.getAt(index).get('id');
            currentOrderNumber = saleReturnedDS.getAt(index).get('order_number');
            var auditingStatus = saleReturnedDS.getAt(index).get('auditing_status');
            var storeStatus = saleReturnedDS.getAt(index).get('store_status');
            returnedEntryFinishItem.setDisabled(true);
            if (auditingStatus == "通过退货" && storeStatus != "入库完毕") {
                returnedEntryFinishItem.setDisabled(false);
            }
            e.preventDefault();
            rightClick.showAt(e.getXY());
        });
        //############ 订单-右键菜单 ##########
        var returnedEntryFinishItem = Ext.create('Ext.menu.Item', {
            text: '【销售退货入库完毕】',
            //iconCls: 'album',
            handler: rightMenuReturnedDeliveryFn
        });

        function rightMenuReturnedDeliveryFn() {
            returnedEntryWin = Ext.create('Ext.window.Window', {
                layout: 'fit',
                width: 307,
                height: 133,
                closeAction: 'destroy',
                plain: true,
                modal: true,
                constrain: true,
                //modal: true,
                title: '销售退货入库完毕确认',
                autoLoad: {
                    url: "/sale_returned/entryConfirm?id=" + saleReturnedId,
                    scripts: true,
                    callback: function (options, success, response, request) {
                        if (response.responseText === "NoPrivilege") {
                            returnedEntryWin.close();
                        }
                    }
                }
            });
            returnedEntryWin.on("destroy", function () {
                saleReturnedDS.load();
            });
            returnedEntryWin.resizable = false;
            returnedEntryWin.show();
        }

        var rightClick = Ext.create('Ext.menu.Menu', {
            items: [returnedEntryFinishItem]
        });


        //--------------------------销售退货订单详细，双击后可显示数据----------------
        //销售退货订单Grid Store Detail
        var saleReturnedDetailDS = Ext.create('Ext.data.Store', {
            //autoDestroy : true,
            model: 'SaleReturnedDetailModel',
            proxy: {
                type: 'ajax',
                url: '/sale_returned/getSaleReturnedDetailsById',
                reader: {
                    type: 'json',
                    root: 'root',
                    totalProperty: 'totalProperty'// 数据集记录总数
                }
            }
        });

        //生成要订单的产品的Grid
        var saleReturnedDetailGrid = Ext.create('Ext.grid.Panel', {
            title: '订单详细列表',
            store: saleReturnedDetailDS,
            columns: [Ext.create('Ext.grid.RowNumberer'), {
                header: 'DB',
                dataIndex: 'id',
                width: 50
            }, {
                header: '产品名称',
                dataIndex: 'product_name',
                width: 200
            }, {
                header: '编号', dataIndex: 'product_number', width: 100
            }, {
                header: '规格',
                dataIndex: 'spec',
                width: 80
            }, {
                header: '数量',
                dataIndex: 'quantity',
                width: 80
            }, {
                header: '单价',
                dataIndex: 'unit_price',
                align: 'right',
                renderer: moneyPrivilegeRender,
                width: 70
            }, {
                header: '折扣',
                dataIndex: 'discount',
                renderer: moneyPrivilegeRender,
                renderer: Ext.util.Format.numberRenderer('0,000.000000'),
                width: 70
            }, {
                header: '总价',
                dataIndex: 'amount',
                align: 'right',
                renderer: moneyPrivilegeRender,
                width: 70
            }, {
                header: '套装或单品',
                dataIndex: 'is_suit',
                width: 70
            }, {
                header: '备注',
                dataIndex: 'description',
                width: 150
            }],
            loadMask: true,
            width: screenWidth - 165,
            height: dgHeight,
            frame: true
        });

        //总的panel
        var workbenchPanel = Ext.create('Ext.form.Panel', {
            width: screenWidth - 165,
            height: totalHeight,
            frame: false,
            autoScroll: true,
            layout: {type: 'table', columns: 1},

            items: [queryFormPanel, saleReturnedGrid, saleReturnedDetailGrid]
        });
        workbenchPanel.render("workbench_panel");
    })
</script>
</head>
<body>
<div id="workbench_panel"></div>
</body>
</html>